package org.linlinjava.litemall.admin.job;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.task.ScheduledCronTaskRegistrar;
import org.linlinjava.litemall.db.domain.LitemallPosAd;
import org.linlinjava.litemall.db.service.LitemallPosAdService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
@RequiredArgsConstructor
public class WebPosAdJob implements Job {

    private final LitemallPosAdService litemallPosAdService;

    private final ScheduledCronTaskRegistrar scheduledCronTaskRegistrar;

    private String cron = "0 0 1 * * ?";


    /**
     * 设置pos外屏广告过期
     */
    //@Scheduled(cron = "0 0 1 * * ?")
    public void WebPosAdExamine(){
        List<LitemallPosAd> posAds = litemallPosAdService.list().stream()
                .filter(pasAd -> !(LocalDateTime.now().isAfter(pasAd.getStartTime()) && LocalDateTime.now().isBefore(pasAd.getEndTime())))
                .map(pasAd -> {
                    pasAd.setIsOpen(0);
                    pasAd.setUpdateTime(LocalDateTime.now());
                    return pasAd;
                })
                .collect(Collectors.toList());
        boolean b = litemallPosAdService.updateBatchById(posAds);
        //litemallPosAdService.posAdPastDue();
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        this.WebPosAdExamine();
        scheduledCronTaskRegistrar.scheduleJobLog(jobExecutionContext);
    }
}
